package com.IO.ReadAndWrite;

import java.io.*;

/***
 * @ClassName: TransStream
 * @Description: 转换流的使用
 * @Auther: 送一一块二向箔
 * @Date: 13:15 2022/2/7
 * @version : V1.0
 */
public class TransStream {
    public static void main(String[] args) {
        diskToMemory();
        memoryToDisk();
    }

    //字节流到字符流
    public static void diskToMemory(){
        try {
            //创建流
            FileInputStream is = new FileInputStream("D:\\aaa.txt");
            //设置编码模式
            InputStreamReader isr = new InputStreamReader(is,"UTF-8");
            //读取文件
            int data = 0;
            while (((data = isr.read()) != -1)) {
                System.out.println((char)data );
            }
            isr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //字符流到字节流
    public static void memoryToDisk(){
        try {
            //创建流
            FileOutputStream is = new FileOutputStream("D:\\memoryToDisk.txt");
            //设置编码模式
            OutputStreamWriter osw = new OutputStreamWriter(is, "GBK");
            //写入文件
            osw.write("大家好\n");
            osw.write("你好世界");

            osw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
